<!DOCTYPE html>
<html>
    <head>
        <title>Verify MediaKeySession.keyStatuses handles multiple keys</title>
        <script src="encrypted-media-utils.js"></script>
        <script src="../../resources/testharness.js"></script>
        <script src="../../resources/testharnessreport.js"></script>
    </head>
    <body>
        <script>
            async_test(function(test)
            {
                var mediaKeySession;
                var initDataType;
                var initData;

                // Create a list of multiple unique keyIds.
                const keyIds = [
                    { keyId: stringToUint8Array('keyid'), status: 'usable' },
                    { keyId: stringToUint8Array('a really long keyid'), status: 'usable' },
                    { keyId: new Uint8Array([0,1,2,3]), status: 'usable' },
                    { keyId: new Uint8Array([0,1,2,3,4]), status: 'usable' },
                    { keyId: new Uint8Array([0,1,2,3,4,5]), status: 'usable' },
                    { keyId: new Uint8Array([0,1,2,3,4,6]), status: 'usable' },
                    { keyId: stringToUint8Array('7'), status: 'usable' },
                    { keyId: stringToUint8Array('8'), status: 'usable' },
                    { keyId: stringToUint8Array('9'), status: 'usable' },
                    { keyId: stringToUint8Array('10'), status: 'usable' },
                    { keyId: stringToUint8Array('11'), status: 'usable' },
                    { keyId: stringToUint8Array('12'), status: 'usable' },
                    { keyId: stringToUint8Array('13'), status: 'usable' },
                    { keyId: stringToUint8Array('14'), status: 'usable' },
                    { keyId: stringToUint8Array('15'), status: 'usable' },
                    { keyId: stringToUint8Array('16'), status: 'usable' },
                    { keyId: stringToUint8Array('17'), status: 'usable' },
                    { keyId: stringToUint8Array('18'), status: 'usable' },
                    { keyId: stringToUint8Array('19'), status: 'usable' },
                    { keyId: stringToUint8Array('20'), status: 'usable' },
                    { keyId: stringToUint8Array('21'), status: 'usable' },
                    { keyId: stringToUint8Array('22'), status: 'usable' },
                    { keyId: stringToUint8Array('23'), status: 'usable' },
                    { keyId: stringToUint8Array('24'), status: 'usable' },
                    { keyId: stringToUint8Array('25'), status: 'usable' }
                ];


                function processKeyStatusesChange(event)
                {
                    // All 25 keyIds should be returned in keyStatuses.
                    verifyKeyStatuses(mediaKeySession.keyStatuses, keyIds);
                    test.done();
                }

                function processMessage(event)
                {
                    // No keys added yet.
                    assert_equals(mediaKeySession.keyStatuses.size, 0);

                    waitForEventAndRunStep('keystatuseschange', mediaKeySession, processKeyStatusesChange, test);

                    // Use the same key for all 25 keyIds.
                    var rawKey = new Uint8Array([0xeb, 0xdd, 0x62, 0xf1, 0x68, 0x14, 0xd2, 0x7b,
                                                 0x68, 0xef, 0x12, 0x2a, 0xfc, 0xe4, 0xae, 0x3c]);
                    var jwks = keyIds.map(function(item) { return createJWK(item.keyId, rawKey); });
                    var jwkSet = stringToUint8Array(createJWKSet.apply(this, jwks));

                    mediaKeySession.update(jwkSet).catch(function(error) {
                        forceTestFailureFromPromise(test, error);
                    });
                }

                navigator.requestMediaKeySystemAccess('org.w3.clearkey', getSimpleConfiguration()).then(function(access) {
                    initDataType = access.getConfiguration().initDataTypes[0];
                    initData = getInitData(initDataType);
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    mediaKeySession = mediaKeys.createSession();

                    // There should be no keys defined yet.
                    assert_equals(mediaKeySession.keyStatuses.size, 0);

                    waitForEventAndRunStep('message', mediaKeySession, processMessage, test);
                    return mediaKeySession.generateRequest(initDataType, initData);
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error);
                });
            }, 'Verify MediaKeySession.keyStatuses handles multiple keys.');
        </script>
    </body>
</html>
